package com.ld.security.common.core.util;

import com.ld.security.common.core.base.entity.ELangEntity;
import com.ld.security.common.core.constant.LangConstant;

import java.lang.reflect.Field;
import java.util.Collection;

public class LangUtil {


    /**
     * 内置转换
     * @param obj
     * @param langId
     * @return
     */
     public static Object langTransform(Object obj, LangConstant langId){
        if(obj instanceof String){
            String str = obj.toString();
            if(langId == LangConstant.zh_simp){
               // str =  HanLP.convertToSimplifiedChinese(str);
            }else{
               // str =  HanLP.convertToTraditionalChinese(str);
            }
            obj=str;
        }else if(obj instanceof Collection){
            Collection cols = (Collection)obj;
            Collection cols2 =   ReflectionUtil.newInstance(cols.getClass());

            for (Object col : cols) {
                if(col instanceof ELangEntity){
                    Object v = langTransform(col,langId);
                    cols2.add(v);
                }
            }
            obj=cols2;
        }else if(obj instanceof ELangEntity){
            for (Field field : ReflectionUtil.getAllDeclaredFields(obj.getClass())) {
                Object v =  ReflectionUtil.invokeGetter(obj,field.getName());
                if(v == null){
                    continue;
                }
                v = langTransform(v,langId);
                ReflectionUtil.invokeSetter(obj,field.getName(),v);
            }

        }
         return  obj;
    }
}
